<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    var people = '苏寿银';
    console.log(people);

    var array = ['苏寿银','徐海城','陈锐佳','麦俊成','江信荫','黄延飞'];
    console.log(array);
    console.log(array[3]);
    console.log(array[1]);

    var array1 = new Array();
    array1[0] = '苏寿银';
    array1[1] = '徐海城';
    array1[2] = '陈锐佳';
    array1.push('麦俊成');
    array1.push('江信荫');
    array1.push('黄延飞');
    array1.push('郑君全');
    console.log(array1);
    array1.pop();
    console.log(array1);

    // 冒泡算法
    var arr = [20,6,8,4,25,7,18];
    var temp = arr[0];
    for (var i = 0; i <= array.length-1; i++) {  /*总共比对多少趟*/
        // console.log(arr[i]);
        for (var j = 0; j < array.length - i; j++) {
            if(arr[j] > arr[j+1]){
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    console.log(arr);
    // temp = 0
    // arr[j] = 20 > arr[j+1] = 6   
    // temp = arr[j] = 20;
    // arr[j] = arr[j+1] = 6;
    // arr[j+1] = temp = 20;

    // a = 2; b = 1;
    // c = 0;

    // c = a;  2

    // a = b;  1
    // b = c;  2

    
    // 二分法
    var two_arr = [3,2,23,4,9,1,44,34,9];
    // 中间值 = two_arr[4] = 9
    // [3,2,4,1]                                  [23,44,34,9]
    // 中间值 = two_arr[2] = 4                     中间值 =  two_arr[2] = 34 
    // [1]              [3,2]                               [23,9]                 [44]
    //             中间值 = two_arr[1] = 2           中间值 = two_arr[1] = 9
    //                 [3]                                                  [23]
    // [1,2,3,4,9,9,23,34,44]

    var a = 1,b = 2;
    var c = a + b;
    // 封装方法名  a,b形参，只是定义了这个方法需要传入两个参数
    function addnum(a,b){
        var c = a + b;
        return c;
    }

    // 使用方法:方法名();
    var d = addnum(5,addnum(3,4));
    console.log(d);
    var f = addnum(a,b)
    console.log(f);

    var array1 = [1,2];
    var array2 = [3,4,5]
    var array3 = array1.concat(array2);
    console.log(array1,array3);
    console.log(array3.splice(3,2));
    // [1] [2] [3]
    // [1,2,3]



    function twoSort(arr){
        //假如数组的元素小于等于1的时候直接返回
        if(arr.length<=1){
            return arr;
        }
        //获取中间值的下标，弹出对应的数组
        var middle = arr.splice(Math.floor(arr.length/2),1);
        console.log(middle);
        //创建左边的空数组
        var leftArr = [];
        //创建右边的空数组
        var rightArr = [];

        for(var i=0; i<arr.length; i++){
            //判断循环里面当前的数字跟中间值的大小
            if(arr[i]<=middle){
                leftArr.push(arr[i]);      //把比中间值小的放一个数组
            }else{
                rightArr.push(arr[i]);     //把比中间值大的放另一个数组
            }
        }
        //递归操作：自己内部调用自己
        var res = twoSort(leftArr).concat(middle,twoSort(rightArr));
        return res;
    }
    var newArr = twoSort(two_arr);
    console.log(newArr);
</script>
</html>